import { view } from 'cc';
import { LaunchData } from '../data/LaunchData';
import { HurdlesData } from '../data/hurdles/HurdlesData';
import { ScreenSize } from './DeviceAttribute';
import { SceneStack } from './SceneStack';
import { UserAttribute } from './UserAttribute';
import EarnCoinData from './EarnCoinData';

class UserGameOutLookConfig {
  public ganName: string = 'DefaultQiuGan';
  public tableName: string = 'PerfectTable';
  public mainBallName: string = 'DefaultMainBall';
  public roomName: string = 'DefaultRoom';
  public date: string = new Date().toDateString();
  constructor() {
    this.tableName = 'DefaultTable';
    const index = Math.floor(Math.random() * 100) % 4;
    if (index === 1) {
      this.tableName = 'HighLevelTable';
    } else if (index === 2) {
      this.tableName = 'LuxuryTable';
    } else if (index === 3) {
      this.tableName = 'PerfectTable';
    }
    this.date = new Date().toDateString();
  }
  public updateQiuGanName(name: string) {
    this.ganName = name;
  }
  public updateTableName(name: string) {
    this.tableName = name;
  }
  public updateMainBallName(name: string) {
    this.mainBallName = name;
  }
  public updateRoomName(name: string) {
    this.roomName = name;
  }
  public parseValue(cvalue: any) {
    if (cvalue.hasOwnProperty('tableName')) {
      this.tableName = cvalue.tableName;
    }
    if (cvalue.hasOwnProperty('roomName')) {
      this.roomName = cvalue.roomName;
    }
    if (cvalue.hasOwnProperty('ganName')) {
      this.ganName = cvalue.ganName;
    }
    if (cvalue.hasOwnProperty('date')) {
      this.date = cvalue.date;
    }
    const today = new Date().toDateString();
    if (this.date !== today) {
      this.date = today;
      this.ganName = 'DefaultQiuGan';
      this.tableName = 'PerfectTable';
      this.roomName = 'DefaultRoom';
    }
  }
}

class GameSettingConfig {
  public musicOn = 1;
  public soundOn = 1;
}

class GameLockConfig {
  public onFriendGame = 0;
  public onNineGame = 0;
  public onSnookerGame = 0;
  public onyouyouGame =0;

  public parseValue(cvalue: any) {
    if (cvalue.hasOwnProperty('onFriendGame')) {
      this.onFriendGame = cvalue.onFriendGame;
    }
    if (cvalue.hasOwnProperty('onNineGame')) {
      this.onNineGame = cvalue.onNineGame;
    }
    if (cvalue.hasOwnProperty('onSnookerGame')) {
      this.onSnookerGame = cvalue.onSnookerGame;
    }

    if (cvalue.hasOwnProperty('onyouyouGame')) {
      this.onyouyouGame = cvalue.onyouyouGame;
    }


  }
}
class RewordVideoAdData {
  public adType = 0; // 0 免费看视频赚金币  1 签到赚金币  2 每日福利赚金币
  public coin = 3000;
}

class ShowScene {
  public showRewardBonus = false;
  public isFromSideBar = false;
  public hasShowBar = false;
}
export class Databus {
  private static instance = null as unknown as Databus;
  public selfInfo: UserAttribute = null;
  public robots: UserAttribute[] = [];
  public rankingList: UserAttribute[] = [];
  public sceneStack: SceneStack = new SceneStack();
  public hurdles: HurdlesData = new HurdlesData(0);//25
  public launchData: LaunchData = new LaunchData();
  public maxContinueHitBall = 0;
  public earnCoin: EarnCoinData = new EarnCoinData();
  public sceenSize = new ScreenSize();
  public gameSetting = new GameSettingConfig();
  public GameLockConfig = new GameLockConfig();
  public outlook: UserGameOutLookConfig = new UserGameOutLookConfig();
  public rad = new RewordVideoAdData();
  public showScene = new ShowScene();
  public view3DMode: number = 0; // 默认视角
  public aiLevel = 1;
  public static Instance() {
    if (!Databus.instance) {
      Databus.instance = new Databus();
    }
    return Databus.instance;
  }

  constructor() {
    // 用户个人信息
    this.selfInfo = UserAttribute.newDefaultUser();
    const vs = view.getVisibleSize();
    this.sceenSize.initScreenSize(vs.width, vs.height);
  }
  public updateUserAttribute() {
    this.aiLevel = this.selfInfo.playLevel.aiPlayLevel();
    if (this.launchData.roomId !== '') {
      this.sceneStack.toMatchPage();
    }
    // 新用户用最厉害的球杆
    if (this.selfInfo.isNewUser()) {
      this.outlook.updateQiuGanName("PerfectQiuGan");
    }
  }
  // 更新机器人信息
  public updateRobotUserList(body: any) {
    this.robots.length = 0;
    for (const user of body['userList']) {
      const ua = new UserAttribute();
      ua.setUserInfo(user);
      this.robots.push(ua);
    }
  }
  // 更新机器人信息
  public updateUserRankingList(body: any) {
    this.rankingList.length = 0;
    for (const user of body['userList']) {
      const ua = new UserAttribute();
      ua.setUserInfo(user);
      this.rankingList.push(ua);
    }
  }
  public updateBagPack(name: string) {
    if (name.indexOf('QiuGan') >= 0) {
      this.outlook.updateQiuGanName(name);
    } else if (name.indexOf('Table') >= 0) {
      this.outlook.updateTableName(name);
    } else if (name.indexOf('Room') >= 0) {
      this.outlook.updateRoomName(name);
    }
  }
  public updateEarnCoin(body: string) {
    const setValue = JSON.parse(body);
    this.earnCoin.parseValue(setValue);
  }
  public updateParseBagPack(body: string) {
    const setValue = JSON.parse(body);
    this.outlook.parseValue(setValue);
  }
  public updateBgMusic(sw: number) {
    this.gameSetting.musicOn = sw;
  }
  public changeAiLevel(wl: number) {
    const aiv = this.aiLevel + wl;
    if (aiv <= 0) {
      this.aiLevel = 0;
    }
    if (aiv >= 5) {
      this.aiLevel = 5;
    }
  }

  public hasAimHoleView() {
    return this.view3DMode === 0;
  }
  public hasAuthHeaderName() {
    return (this.selfInfo.avatarUrl !== "" && this.selfInfo.nickName !== "")
  }
}